/**
* @Title: MybatisPlusConfig.java
* @Package com.reguang.common.config.datasourceConfig
* @Description: (描述该文件做什么)
* @author Administrator
* @date 2020年2月9日 下午3:25:04
* @version V1.0
*/
package com.reguang.datasourceConfig;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
 * Title: MybatisPlusConfig
 * <p>
 * Description:
 * </p>
 * 
 * @author 路汉腾 2020年2月9日 下午3:25:04
 */
@Configuration
public class DatasourceConfig {


	@Bean(name = "db1")
	@ConfigurationProperties(prefix = "spring.datasource.master")
	public DataSource db1() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean(name = "db2")
	@ConfigurationProperties(prefix = "spring.datasource.slave1")
	public DataSource db2() {
		return DruidDataSourceBuilder.create().build();
	}

	@Bean(name = "db3")
	@ConfigurationProperties(prefix = "spring.datasource.slave2")
	public DataSource db3() {
		return DruidDataSourceBuilder.create().build();
	}

	/**
	 * 动态数据源配置
	 *
	 * @return
	 */
	@Bean
	@Primary
	public DataSource multipleDataSource(@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2,
			@Qualifier("db3") DataSource db3) {
		DynamicDataSource dynamicDataSource = new DynamicDataSource();
		Map<Object, Object> targetDataSources = new HashMap<>();
		targetDataSources.put(DbTypeEnum.db1.getValue(), db1);
		targetDataSources.put(DbTypeEnum.db2.getValue(), db2);
		targetDataSources.put(DbTypeEnum.db3.getValue(), db3);
		dynamicDataSource.setTargetDataSources(targetDataSources);
		dynamicDataSource.setDefaultTargetDataSource(db1);
		DbContextHolder.setDbType(DbTypeEnum.db1);
		return dynamicDataSource;
	}
	
	@Bean
	public ServletRegistrationBean druidServlet() {
		ServletRegistrationBean reg = new ServletRegistrationBean();
		reg.setServlet(new StatViewServlet());
		reg.addUrlMappings("/druid/*");
		reg.addInitParameter("allow", ""); // 白名单
		return reg;
	}

	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		filterRegistrationBean.addInitParameter("profileEnable", "true");
		filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
		filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
		filterRegistrationBean.addInitParameter("DruidWebStatFilter", "/*");
		return filterRegistrationBean;
	}

	
}
